var ylib = function() {
    return {
        util: {},
        widget: {},
        namespace: function(sNameSpace) {
            if (!xStr(sNameSpace)) return null;
            var levels = sNameSpace.split('.');
            var thisNameSpace = ylib;
            for (var i = (levels[0] == 'ylib' ? 1: 0); i < levels.length; i++) {
                thisNameSpace[levels[i]] = thisNameSpace[levels[i]] || {};
                thisNameSpace = thisNameSpace[levels[i]];
            }
        }
    };
} ();

ylib.namespace('ylib.util');
//ylib.util.READY_STATE_UNINITIALIZED = 0;
//ylib.util.READY_STATE_LOADING = 1;
//ylib.util.READY_STATE_LOADED = 2;
//ylib.util.READY_STATE_INTERACTIVE = 3;
ylib.util.READY_STATE_COMPLETE = 4;
ylib.util.DefaultErrorHandler = function (request) {
   /* var mess = "error fetching data!";
    if (this) {
        if (this.url) mess += "\n\nurl:" + this.url;
        if (this.method) mess += "\n\nmethod:" + this.method;
        if (this.contentType) mess += "\n\ncontentType:" + this.contentType;
        if (this.params) mess += "\n\nparams:" + this.params;
        if (this.error) {
            if (this.error.name) mess += "\n\nerror name:" + this.error.name;
            if (this.error.message) mess += "\n\nerror message:" + this.error.message;
        }
    }
    if (request) {
        if (request.readyState) mess += "\n\nreadyState:" + request.readyState;
        try {
            if (request.status) mess += "\n\nstatus:" + request.status;
        } catch (err) {}
        try {
            if (request.getAllResponseHeaders) mess += "\n\nheaders:" + request.getAllResponseHeaders();
        } catch (err) {}
    }
    alert(mess);*/
};
ylib.util.ContentLoader = function (component, url, callBackHandler, errorHandler, method, params, contentType) {
    this.component = component;
    this.url = url;
    this.callBackHandler = callBackHandler;
    this.errorHandler = (errorHandler) ? errorHandler : ylib.util.DefaultErrorHandler;
    this.method = (method) ? method : 'GET';
    this.params = (params) ? params : [];
    this.contentType = contentType;
    if (!contentType && method == "POST") {
        this.contentType = 'application/x-www-form-urlencoded';
    }
};
ylib.util.ContentLoader.prototype = {
    SendRequest: function () {
        var request = this.GetTransport();
        if (request) {
            var requestParams = [];
            for (var i = 0; i < arguments.length; i++) {
                requestParams.push(arguments[i]);
            }
            var loader = this;
            var url = this.url;
            if (this.method == 'GET' && requestParams.length > 0) {
                var qs = this.GetFullQuerystring(requestParams);
                if (qs) {
                    if (url.indexOf('?') == -1) {
                        url += '?' + qs;
                    } else {
                        url += '&' + qs;
                    }
                }
            }
            try {
                request.onreadystatechange = function () {
                    loader.HandleAjaxResponse.call(loader, request);
                };
                request.open(this.method, url, true);
                if (this.contentType) request.setRequestHeader('Content-Type', this.contentType);
                request.setRequestHeader('encoding', 'UTF-8');
                if (this.method == 'GET') {
                    request.send(null);
                } else {
                    request.send(this.GetFullQuerystring(requestParams));
                }
            } catch (err) {
                loader.error = err;
                this.errorHandler.call(loader, request);
            }
        }
    },
    GetTransport: function () {
        var transport;
        if (window.XMLHttpRequest) {
            transport = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            transport = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return transport;
    },
    GetFullQuerystring: function (args) {
        var requestParams = [];
        for (var i = 0; i < this.params.length; i++) {
            if (this.params[i]) requestParams.push(this.params[i]);
        }
        for (var i = 0; i < args.length; i++) {
            if (args[i]) requestParams.push(args[i]);
        }
        var qs = "";
        if (requestParams && requestParams.length > 0) {
            qs = requestParams.join("&");
        }
        return qs;
    },
    HandleAjaxResponse: function (request) {
        var loader = this;
        if (request.readyState == ylib.util.READY_STATE_COMPLETE) {
            if (loader.RequestSuccess(request)) {
                loader.callBackHandler.call(loader, request);
            } else {
                loader.errorHandler.call(loader, request);
            }
            request = null;
        }
    },
    RequestSuccess: function (request) {
        return request.status == 0 || (request.status >= 200 && request.status < 300);
    },
    ClearUp: function () {
        this.component = null;
        this.callBackHandler = null;
        this.errorHandler = null;
    }
};